package com.linkhub.oms.admin.modules.track.util;

import com.linkhub.oms.admin.modules.order.domain.vo.OrderTabCountVO;
import com.linkhub.oms.admin.modules.order.domain.vo.orderlist.OrderlistVO;
import com.linkhub.oms.admin.modules.order.util.OrderMappingAssist;
import com.linkhub.oms.admin.modules.track.domain.vo.OrderTrackEventVo;
import com.linkhub.oms.admin.modules.track.domain.vo.OrderTrackListVo;
import com.linkhub.oms.common.enums.mp.BoolEnum;
import com.linkhub.oms.common.util.QtBeanUtil;
import com.linkhub.oms.common.util.QtStreamUtil;
import com.linkhub.oms.entity.OrderEntity;
import com.linkhub.oms.entity.OrderTrackEntity;
import com.linkhub.oms.entity.innerproperties.TrackEventInfoEntity;
import com.linkhub.oms.entity.resulttype.order.OrderInfoDO;

import java.util.List;

/**
 * @author Redball
 * @version 1.0.0
 * @Description OrderTrackMappingAssist
 * @createTime 2024/10/26 下午5:34
 */
public class OrderTrackMappingAssist {

    public static OrderTrackListVo mappingTrackOrderlist(OrderInfoDO orderInfoDO) {
        OrderEntity order = orderInfoDO.getOrder();
        OrderTrackEntity orderTrackEntity = orderInfoDO.getOrderTrack();

        OrderlistVO orderlistVO = OrderMappingAssist.mappingCommonOrderlist(orderInfoDO);
        orderlistVO.setOrderStatus(order.getEmployeeOrderStatus().getCode());
        orderlistVO.setOrderStatusDesc(order.getEmployeeOrderStatus().getDesc());
        orderlistVO.setSplitFlag(BoolEnum.NO.getCode());
        orderlistVO.setWayBillNumber(order.getWayBillNumber());

        OrderTrackListVo orderTrackListVo = new OrderTrackListVo();
        QtBeanUtil.copyProperties(orderlistVO,orderTrackListVo);

        if(orderTrackEntity != null) {
            orderTrackListVo.setTrackLatestEvent(orderTrackEntity.getLatestEventDescription());
            orderTrackListVo.setTrackLatestTime(orderTrackEntity.getLatestEventTimeUtc());
            orderTrackListVo.setTrackLatestLocation(orderTrackEntity.getLatestEventLocation());
            orderTrackListVo.setTrackTransitDays(orderTrackEntity.getDaysOfTransit());
            orderTrackListVo.setTrackFailDesc(orderTrackEntity.getLatestSubStatusDescr());
            List<TrackEventInfoEntity> trackEventInfoEntities = QtBeanUtil.copyList(orderTrackEntity.getTrackEvents(), TrackEventInfoEntity::new);
            orderTrackListVo.setTrackEvents(QtStreamUtil.map(trackEventInfoEntities, OrderTrackMappingAssist::mappingTrackEventVo));
        }
        return orderTrackListVo;
    }

    public static OrderTrackEventVo mappingTrackEventVo(TrackEventInfoEntity trackEventInfoEntity) {
        OrderTrackEventVo vo = new OrderTrackEventVo();
        vo.setEvent(trackEventInfoEntity.getDescription());
        vo.setTime(trackEventInfoEntity.getTimeUtc());
        vo.setLocation(trackEventInfoEntity.getLocation());
        return vo;
    }

    public static OrderTabCountVO mappingTrackTabCount(OrderEntity order) {
        OrderTabCountVO vo = new OrderTabCountVO();
        Integer code = order.getTrackBusinessStatus().getCode();
        vo.setTabId(code);
        vo.setCount(order.getCount());

        return vo;
    }
}
